package com.zto.tbschedule.console.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import com.zto.tbschedule.console.mapper.AppUserModelMapper;
import com.zto.tbschedule.console.mapper.UserModelMapper;
import com.zto.tbschedule.console.model.AppUserModelExample;
import com.zto.tbschedule.console.model.UserModel;
import com.zto.tbschedule.console.model.UserModelExample;
import com.zto.tbschedule.console.service.IUserService;
import com.zto.tbschedule.console.util.MD5Util;
import com.zto.tbschedule.console.vo.User;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserModelMapper userModelMapper;


    @Autowired
    private AppUserModelMapper appUserModelMapper;

    public void addUser(User user) {
        UserModel model = new UserModel();
        if (user.getRole() != null && user.getRole().equals("管理员")) {
            model.setIsAdmin(1);
        } else {
            model.setIsAdmin(0);
        }
        model.setPassword(MD5Util.MD5(user.getPassword()));
        model.setUsername(user.getUsername());
        model.setNickName(user.getNickName());
        model.setEnable(1);
        userModelMapper.insertSelective(model);
    }

    public User searchUserByUserName(String userName) {
        User user = searchUserAndPasswdByUserName(userName);
        if (user != null) {
            user.setPassword(null);
        }
        return user;
    }

    public int delUserByUserId(Long id) {
        UserModelExample example = new UserModelExample();
        example.createCriteria().andIdEqualTo(id);
        return userModelMapper.deleteByExample(example);
    }

    public int modifyUser(User user) {
        User oldUser = searchUserAndPasswdByUserName(user.getUsername());
        Assert.isNull(oldUser, "未知的用户");
        UserModel model = new UserModel();
        model.setId(oldUser.getId());
        model.setUsername(oldUser.getUsername());
        if (user.getNewPassword() != null) {
            Assert.isTrue(!oldUser.getPassword().equals(MD5Util.MD5(user.getPassword())), "原密码错误");
            model.setPassword(MD5Util.MD5(user.getNewPassword()));
        }
        if (user.getNickName() != null) {
            model.setNickName(user.getNickName());
        }

        if (user.getRole() != null) {
            model.setIsAdmin(user.getRole().equals("管理员") ? 1 : 0);
        }
        if (user.getEnable() != null) {
            model.setEnable(user.getEnable().equals("启用") ? 1 : 0);
        }

        return userModelMapper.updateByPrimaryKeySelective(model);
    }

    public User login(User user) {
        User userInfo = searchUserAndPasswdByUserName(user.getUsername());
        Assert.notNull(userInfo, user.getUsername() + "用户不存在");
        Assert.isTrue(MD5Util.MD5(user.getPassword()).equals(userInfo.getPassword()), "密码错误");
        userInfo.setPassword(null);
        return userInfo;
    }

    private User searchUserAndPasswdByUserName(String userName) {
        UserModelExample example = new UserModelExample();
        example.createCriteria().andUsernameEqualTo(userName);
        List<UserModel> userModelList = userModelMapper.selectByExample(example);
        if (userModelList != null && userModelList.size() >= 1) {
            UserModel model = userModelList.get(0);
            User user = userModel2User(model);
            return user;
        }
        return null;
    }

    public List<User> listUser() {
        List<User> userList = new ArrayList<User>();
        List<UserModel> userModelList = userModelMapper.selectByExample(new UserModelExample());
        if (userModelList != null && userModelList.size() >= 1) {
            for (UserModel model : userModelList) {
                User user = userModel2User(model);
                user.setPassword(null);
                AppUserModelExample example = new AppUserModelExample();
                example.createCriteria().andUidEqualTo(user.getId());
                user.setAppCount(appUserModelMapper.countByExample(example));
                userList.add(user);
            }
        }
        return userList;
    }

    private User userModel2User(UserModel model) {
        User user = new User();
        user.setId(model.getId());
        user.setUsername(model.getUsername());
        user.setPassword(model.getPassword());
        user.setRole(model.getIsAdmin() == 1 ? "管理员" : "普通用户");
        user.setNickName(model.getNickName());
        user.setEnable(model.getEnable() == 1 ? "启用" : "禁用");
        user.setCreateDate(model.getCreateDate());
        user.setModifyDate(model.getModifyDate());
        return user;
    }

}
